import BaseEvent from '@/components/events/BaseEvent'
import { CONTROL_TYPES } from '@/common/const-wb'
import { replaceStr, isHasOwnProperty, getCompIdByParam } from '@/plugins/utils'

export default class PushEvent extends BaseEvent{
  constructor() {
    super();

    this.isEventPostFail = false;//一个事件返回失败的变量，用来做下一个事件的判断条件，发博成功与失败不同文案
  }
  
  excute({item, event, isEventPostFail}){
    // return { text:"", pic_ids:"", url_:"", isContinue:false }
    this.item = item;
    this.event = event;
    this.isEventPostFail = isEventPostFail;
    let controlData = this.controls[this.item.id][CONTROL_TYPES.wb_push]
    //这个组件不发博，只是给给的组件提供索引数据
    let isContinue = true;//!controlData.data.isGiveOther
    let url_ = '/feact/wb_push'

    let pic_ids_ = []
    let pic_urls_ = []
    let elments = controlData.data.elements
    let isRandomPush = controlData.data.isRandomPush;
    let randomIndex = 0
    const relateCompId = getCompIdByParam(controlData.data.relateCompId)
    const relateCompData = this.componentMap[relateCompId];
    if (isRandomPush) {
      randomIndex = _.random(0, elments.length - 1)
      if(controlData.data.isGiveOther){
        //设置关联组件pushIndex rotateIndex
        if(relateCompId && relateCompData){
          relateCompData.commonAttr.itemIndex = randomIndex;
        }
      }
    } else {
      if(controlData.data.isGetOther){
        if(relateCompData) {
          let relateAttrCompId = getCompIdByParam(this.commonAttr.relateAttrCompId)
          if(relateAttrCompId && relateAttrCompId.length > 0) {
            const relateAttrCompData = this.componentMap[relateAttrCompId];
            if(relateAttrCompData.lists && relateAttrCompData.lists.length > 0) {
              let vote_id = relateAttrCompData.lists[this.commonAttr.itemIndex].voteId;
              randomIndex = vote_id - 1;
            }
          } else if(relateCompData.commonAttr.pushIndex || relateCompData.commonAttr.itemIndex >= 0) {
            randomIndex = relateCompData.commonAttr.pushIndex || (relateCompData.commonAttr.itemIndex >= 0 ? relateCompData.commonAttr.itemIndex : 0);
          } 
        } 
      }else{
        //其他动作赋的值 比如发博设置的pushIndex 或者是这个组件的3d旋转设置的
        if(this.commonAttr.pushIndex >= 0){
          randomIndex = this.commonAttr.pushIndex
        }else{
          randomIndex = this.commonAttr.itemIndex >= 0 ? this.commonAttr.itemIndex : 0 ;
        }
      }
    }
    if(randomIndex >= elments.length){
      randomIndex = elments.length - 1;
    }
    let pushItem_ = elments[randomIndex]
    if(pushItem_ && pushItem_.isPic){
      if (pushItem_.isRandomPic) {
        let randomPicIndex = _.random(0, pushItem_.pics.length - 1)
        let picItem = pushItem_.pics[randomPicIndex]
        if(picItem.pic_id){
          if(!isHasOwnProperty(this.globalIsPost, 'isPushImg') || isHasOwnProperty(this.globalIsPost, 'isPushImg') && this.globalIsPost.isPushImg){
            pic_ids_.push(picItem.pic_id)
          }
          pic_urls_.push(picItem.pic_url)
        }
      } else {
        _.forEach(pushItem_.pics, (picItem) => {
          if(picItem.pic_id){
            if(!isHasOwnProperty(this.globalIsPost, 'isPushImg') || isHasOwnProperty(this.globalIsPost, 'isPushImg') && this.globalIsPost.isPushImg){
              pic_ids_.push(picItem.pic_id)
            }
            pic_urls_.push(picItem.pic_url)
          }
        });
      }
    }
    //在前端动态图片中显示发博的图片，及发博和前端显示的图片是一样的，发博后显示图片 405
    let dynamicPicUrl = '';
    if (controlData.data.isSame) {
      dynamicPicUrl = pic_urls_[0];
    }else{
      dynamicPicUrl = pushItem_ && pushItem_.url;
    }
    const isHaveIsReplaceShareData = isHasOwnProperty(controlData.data, "isReplaceShareData");
    if (((isHaveIsReplaceShareData && controlData.data.isReplaceShareData) || !isHaveIsReplaceShareData)  && dynamicPicUrl) {
      let shareData = { url: dynamicPicUrl };
      this.store.commit('updateShareData', shareData);
    }
    let pic_ids = pic_ids_.join()
    //替换博文中的$id$为对应组件中的内容
    let text = replaceStr(this.formValues, pushItem_.text);
    if (!text){
      this.showToast('亲，发博内容不能为空！')
      url_ = '';
      isContinue = false
      return { text, pic_ids, url_, isContinue }
    }
    if (this.isEventPostFail && controlData.data.failText && controlData.data.failText.length > 0) {
      text = controlData.data.failText
    }
    //发完博后就清除数据
    if(this.canvasImgComponentMap[this.item.id]){
      window.setTimeout(() => {
        pushItem_.pics = []
      }, 2000);
    }
    return { text, pic_ids, url_, isContinue }
  }

  // getPushIndex (pushControlData) {
  //   let elem = null;
  //   let targetItem = this.wbListOrLists[this.commonAttr.itemIndex];
  //   let itemIndex = -1;
  //   if(targetItem.voteId){
  //     itemIndex = parseInt(targetItem.voteId) - 1;
  //   }
  //   for(let i = 0, len = pushControlData.elements.length; i < len; i++){
  //     elem = pushControlData.elements[i];
  //     if(elem.mid && elem.mid === targetItem.mid) {
  //       return i
  //     }else if(elem.uid && elem.uid === targetItem.uid){
  //       return i
  //     }else if(i == itemIndex){
  //       return i
  //     }
  //   }
  // }

  destroy(){
    super.destroy();
  }

}